import os
import pygame
from ui import events
from ui import window, src
from ui.notes import notesui
from ui.defaultset import defaultset
from config import uiconfig
from pm import pm, notes
from tools import *
screen = None
obj_path = ""
opens = []
choose_screen = 0
mode_type = 0
if (config.config.HW_SURFACE):
    mode_type = pygame.HWSURFACE
item_surface = pygame.Surface((1000-64, 600), flags=mode_type)


def scale(surface, scale):
    return pygame.transform.scale(surface, (scale, scale))


def render_menu(render_screen: pygame.Surface):
    global screen, choose_screen, item_surface
    screen = render_screen
    action_list = [
        defaultset,
        notesui
    ]
    if opens[0]["song"] == "":
        menu_list = [
            src.file_menu
        ]
    else:
        menu_list = [
            src.file_menu,
            src.map_menu,
            src.anim_menu,
            src.output_menu
        ]
    pygame.draw.rect(screen, uiconfig.DRAWER_COLOR, (0, 0, 47, 600))
    for i in range(len(menu_list)):
        if i == choose_screen:
            pygame.draw.rect(screen, uiconfig.LIGHT_COLOR, (0, 48*i, 2, 48))
        screen.blit(scale(menu_list[i], 32), (8, 48*i+8))
        if (events.pressmouse and pygame.Rect(0, 48*i, 48, 48).collidepoint(pygame.mouse.get_pos())):
            action_list[choose_screen].on_close()
            info("ui/menu", f"change screen[{i}]")
            choose_screen = i
    item_surface.fill(uiconfig.BACKGROUND_COLOR)
    action_list[choose_screen].render(item_surface)
    screen.blit(item_surface, (64, 0))


def new_project(obj_paths: str):
    window.page = 2
    pm.make_project(obj_paths)
    info("ui/menu", f"new project [{obj_paths}]")


def open_project(obj_paths: str):
    global obj_path, opens
    obj_path = obj_paths
    info("ui/menu", f"open project [{obj_path}]")
    opens = pm.read_project(obj_path)
    opennote = notes.open_all_notes(obj_path)
    defaultset.change_info(opens)
    window.page = 1
